namespace DealerPlatform.Common.RedisModule;

public partial class RedisWorker(RedisCore redisCore) : IRedisWorker
{
    /// <summary>
    /// 通过Scan获取所有适配的Key
    /// </summary>
    /// <param name="key">可以带通配符的Key</param>
    /// <returns></returns>
    public List<string> GetKeys(string key)
    {
        List<string> keyList = [];
        var eps = redisCore.Conn.GetEndPoints();
        var ep = eps[0];
        // 通过EndPoints拿到Redis的服务
        var server = redisCore.Conn.GetServer(ep);
        // 通过Server拿到Redis中所有符合条件的Key
        var keys = server.Keys(0, key).ToList();
        keys.ForEach(k => { keyList.Add(k.ToString()); });
        return keyList;
    }
    
    public void RemoveKey(string key)
    {
        redisCore.Db.KeyDelete(key);
    }
}